﻿#include "initcreathtml.h"
#include <QDirIterator>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
InitCreatHtml::InitCreatHtml(QWidget *parent) : QWidget(parent)
{
    m_savaImportScript.clear();
}

InitCreatHtml::~InitCreatHtml()
{

}
void InitCreatHtml::CreateTempJS()
{

    //创建Temp.js
    m_savaImportScript.clear();
    QString strAppCollect = "var AppCollections = [";
            strAppCollect += "\r\n";
    //获取路径
    QString strAppJsFile("C:/Csap/Nisp/html/appResource");
            //filePath.GetFullHtmlPath(QString());
    //获取所选文件类型过滤器
    QStringList filters;
    //文件过滤
    filters << QString("*.js");
    //定义迭代器并设置过滤器
    QDirIterator dirIterator(strAppJsFile,filters,QDir::Files | QDir::NoSymLinks,QDirIterator::Subdirectories);

    while(dirIterator.hasNext())
        {
            dirIterator.next();
            QFileInfo fileInfo = dirIterator.fileInfo();
            //JS文件名称，不含后缀
            if(fileInfo.isFile() && fileInfo.size()>0)
            {
                QString fileName = fileInfo.baseName();
                m_savaImportScript += QString("<script type=\"text/javascript\" src=\"./appResource/%1.js\"></script>").arg(fileName);
                m_savaImportScript += "\r\n";
                strAppCollect += QString("['%1',{ 'default': %2_pages }],").arg(fileName).arg(fileName);
                strAppCollect += "\r\n";
            }

        }
    if(strAppCollect.endsWith(",\r\n"))
    {
        strAppCollect.chop(3);
    }
    strAppCollect += "\r\n";
    strAppCollect += "]";
    QString tempjsJSONFile(("C:/Csap/Nisp/html/js/temp.js"));

    QFile file;
    file.setFileName(tempjsJSONFile);
    if(!file.exists())
    {
        file.remove();
    }
    QFile newFile(tempjsJSONFile);
    bool isOk = newFile.open(QIODevice::WriteOnly);
    if(!isOk)
    {
        QMessageBox::critical(NULL,toL8B("失败"),toL8B("打开 Temp.js 失败"));
        return;
    }
    //写文件
    QTextStream inFile(&newFile);
    inFile << strAppCollect;
    //关闭文件
    newFile.close();
    emit MainPageTestSignal("CompleteJS");
}
void InitCreatHtml::CreateMainPageTest()
{
    // 读取基础MainPageTest.htm内容,并拼接成mainPageTest.htm
    QString strMainTest("");
    QString strMainPageTest("C:/Csap/Nisp/html/MainPageTest.htm");
    QFile rTestFile(strMainPageTest);
    rTestFile.encodeName("UTF-8");
    bool isOk = rTestFile.open(QIODevice::ReadOnly);
    if(!isOk)
    {
        QMessageBox::critical(NULL,toL8B("失败"),toL8B("读 MainPageBase.htm 失败"));
    }
    char buf[1024] ={0};
    QString strBuf;
    strBuf.clear();
    QString strTarget("<script type=\"text/javascript\" src=\"bundle/A01_common.js\"></script>");
    while (!rTestFile.atEnd()) {
        qint64 lineLength = rTestFile.readLine(buf,sizeof(buf));
        qDebug() << lineLength << " " <<buf;
        if(lineLength !=-1)
        {
            //读取的内容有效
            strMainTest.append(buf);
            //读到目标行结束
            if(strMainTest.indexOf(strTarget) != -1)
            {
                strMainTest +=QString("\r\n");
                break;
            }
        }
    }
    qDebug() << strMainTest;
    strMainTest.append(m_savaImportScript);
    strMainTest.append(QString("\r\n<script type=\"text/javascript\" src=\"%1\"></script>\r\n").arg("./js/temp.js"));
    //将main.js插入
    strMainTest.append(QString("<script type=\"text/javascript\" src=\"bundle/main.js\"></script>"));
    strMainTest.append(QString("\r\n</html>"));
    //关闭文件
    rTestFile.close();

    //将数据写入MainPageTest.htm
    //如果存在MainPageTest.htm则删除
    QFile wTestFile(strMainPageTest);
    wTestFile.encodeName("UTF-8");
    if(wTestFile.exists())
    {
        //存在则删除
        if(!wTestFile.remove())
        {
            QMessageBox::critical(NULL,toL8B("失败"),toL8B("remove MainPageTest.htm  失败!"));
            return;
        }

    }
    bool isOpen = wTestFile.open(QIODevice::WriteOnly);
    if(!isOpen)
    {
        QMessageBox::critical(NULL,toL8B("失败"),toL8B("%1 打开 MainPageTest.htm  失败!"));
    }
    QTextStream  theMainPageTest(&wTestFile);
    theMainPageTest.setCodec("UTF-8");
    theMainPageTest.setGenerateByteOrderMark(true);
    theMainPageTest << strMainTest;
    //关闭文件
    wTestFile.close();

    emit MainPageTestSignal("CompleteHtml");
}

